package org.luxor.storage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.luxor.commons.core.annotation.SysLog;
import org.luxor.commons.core.validator.group.AddGroup;
import org.luxor.commons.core.validator.group.UpdateGroup;
import org.luxor.commons.core.web.controller.BaseRestController;
import org.luxor.commons.core.web.data.R;
import org.luxor.commons.jdbc.metadata.PageQuery;
import org.luxor.storage.entity.OssConfigEntity;
import org.luxor.storage.service.IOssConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;

/**
 * OSS参数配置
 *
 * @author Mr.yan @date 2020/3/15
 */
@RestController
@RequestMapping(value = "/sys/storage/config")
@Api(tags = "OSS参数配置")
public class ConfigController extends BaseRestController {

    @Resource
    private IOssConfigService ossConfigService;

    /**
     * OSS参数配置列表
     */
    @PostMapping("/list")
    @PreAuthorize("hasPermission(#request,'sys:storage:config:list')")
    @ApiOperation("OSS参数配置列表")
    public R<IPage<OssConfigEntity>> list(@RequestBody PageQuery<HashMap> searchPage) {
        IPage<OssConfigEntity> data = ossConfigService.page(searchPage.getPage(), searchPage.getSearch());
        return R.ok(data);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("hasPermission(#request,'sys:storage:config:info')")
    @ApiOperation("获取OSS参数配置信息")
    @ApiImplicitParam(name = "id", value = "OSS参数配置ID", paramType = "path", required = true)
    public R<OssConfigEntity> info(@PathVariable("id") Long id) {
        OssConfigEntity ossConfig = ossConfigService.getById(id);
        return R.ok(ossConfig);
    }

    /**
     * 保存
     */
    @SysLog("保存OSS参数配置")
    @PostMapping("/save")
    @PreAuthorize("hasPermission(#request,'sys:storage:config:save')")
    @ApiOperation("保存OSS文件存储配置")
    public R<OssConfigEntity> save(@Validated(AddGroup.class) @RequestBody OssConfigEntity ossConfig) {
        ossConfig.setUpdateTime(new Date());
        ossConfigService.save(ossConfig);
        return R.ok(ossConfig);
    }

    /**
     * 修改
     */
    @SysLog("修改OSS参数配置")
    @PutMapping("/update")
    @PreAuthorize("hasPermission(#request,'sys:storage:config:update')")
    @ApiOperation("修改OSS文件存储配置")
    public R<OssConfigEntity> update(@Validated(UpdateGroup.class) @RequestBody OssConfigEntity ossConfig) {
        ossConfig.setUpdateTime(new Date());
        ossConfigService.updateById(ossConfig);
        return R.ok(ossConfig);
    }

    /**
     * 删除
     */
    @SysLog("删除OSS参数配置")
    @DeleteMapping("/delete")
    @PreAuthorize("hasPermission(#request,'sys:storage:config:delete')")
    @ApiOperation("删除OSS文件存储配置")
    public R delete(@RequestBody Long[] ids) {
        ossConfigService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
